#include "urlmatch.h"
#include <sys/time.h>

struct urlmatch_userdata {
    int host_len;
    int path_len;
};

int main(int argc, char **argv)
{
    struct urlmatch *m;
    struct urlmatch_userdata *userdata;
    struct timeval start_time;
    struct timeval end_time;

    if (argc < 3) {
        printf("usage: %s doman_suffix_list.dat www.baidu.com.cn\n", argv[0]);
        exit(1);
    }

    m = urlmatch_new(free, URLMATCH_FULL_CHAR_LIST, URLMATCH_FULL_CHAR_LIST_LEN);
    //m = urlmatch_new(free, URLMATCH_DEFAULT_CHAR_LIST, URLMATCH_DEFAULT_CHAR_LIST_LEN);
    FILE *fp = fopen(argv[1], "r"); if (fp == NULL) { printf("fail to fopen(%s, r)\n", argv[1]); exit(1); }
    char line[512];
    char subfix[128];

    while (fgets(line, sizeof(line), fp)) {
        if (strlen(line) > 0 && line[strlen(line)-1] == '\r') line[strlen(line)-1] = 0;
        if (strlen(line) > 0 && line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
        if (strlen(line) <= 0) continue;
        if (line[0] == '/')  continue;
        sprintf(subfix, ".%s", line);
        //printf("loading %s\n", subfix);
        userdata = calloc(1, sizeof(*userdata)); userdata->host_len = strlen(subfix) , userdata->path_len = -1; urlmatch_add_url_extra(m, subfix, strlen(subfix), NULL, 0, userdata, 0);
    }
    fclose(fp);
    //userdata = calloc(1, sizeof(*userdata)); userdata->host_len = 7 , userdata->path_len = -1; urlmatch_add_url(m, ".com.cn", 7, NULL, 0, userdata, 0);
    urlmatch_stat(m);

    uint32_t deal_count = 0;
    fp = fopen(argv[2], "r");
    if (fp == NULL) { printf("fail to fopen(%s, r)\n", argv[2]); exit(1); }
    gettimeofday(&start_time, NULL);
    while (fgets(line, sizeof(line), fp)) {
        if (strlen(line) > 0 && line[strlen(line)-1] == '\r') line[strlen(line)-1] = 0;
        if (strlen(line) > 0 && line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
        if (strlen(line) > 0 && line[strlen(line)-1] == '\r') line[strlen(line)-1] = 0;
        if (strlen(line) > 0 && line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
        if (strlen(line) <= 0) continue;

        char *host = line;
        int host_len = strlen(line);
        urlmatch_pattern_t *pattern = urlmatch_find_long_host(m, host, host_len);
        if (pattern) {
            userdata = pattern->userdata;
            char *subfix = host + host_len - userdata->host_len; int subfix_len = userdata->host_len;
            char *domain = memrchr(host, '.', host_len - subfix_len); if (domain == NULL) domain = host; int domain_len = subfix - domain;
            char *domain_clear;
            if (domain_len > 0 && domain[0] == '.') { domain_clear = domain+1; domain_len--; }
            else                                      domain_clear = domain;
            int subdomain_len = domain - host;
            printf("host %-64s subdomain %-48.*s domain %-32.*s subfix %-32.*s\n", host, subdomain_len, host, domain_len, domain_clear, subfix_len-1, subfix+1);
        }
        deal_count++;
    }
   
    gettimeofday(&end_time, NULL);
    printf("start_time sec %u usec %u end_time sec %u usec %u [usec dis %u], deal_host %u\n", start_time.tv_sec, start_time.tv_usec, end_time.tv_sec, end_time.tv_usec, end_time.tv_usec - start_time.tv_usec, deal_count);
    urlmatch_free(m);
    return 0;
}
